package com.hehao.kurunner.ui;

import android.content.pm.PackageInfo;
import android.os.Handler;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hehao.hehaolibrary.util.Constant;
import com.hehao.hehaolibrary.util.HHShare;
import com.hehao.hehaolibrary.util.HHUtils;
import com.hehao.kurunner.R;
import com.hehao.kurunner.common.BaseActivity;
import com.hehao.kurunner.common.BaseApplication;

/**
 * 启动页面
 * @author HeHao
 * @time 2015/11/13 15:27
 * @email 139940512@qq.com
 */
public class LaunchActivity extends BaseActivity implements Runnable{

    /**欢迎页面图片*/
    private ImageView img ;
    /**是否进入引导页面*/
    private boolean isEnterGuide;


    @Override
    protected void initView() {
        img = new ImageView(this);
        img.setImageResource(R.mipmap.launch_bg);
        img.setScaleType(ImageView.ScaleType.FIT_XY);

        setContentView(img, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    protected void initData() {
        int lastVersionCode = HHShare.getInstance(this).getInt(Constant.LAST_VERSION) ;
        PackageInfo packageInfo = HHUtils.getAppVersion(this);
        isEnterGuide = false ;

        if(packageInfo != null && packageInfo.versionCode != lastVersionCode) {
            HHShare.getInstance(this).putInt(Constant.LAST_VERSION, packageInfo.versionCode);
            isEnterGuide = true;
        }

        Handler handler = new Handler();
        handler.postDelayed(this, 3000);
    }

    @Override
    public void run() {
        if(isEnterGuide)
            startActExit(GuideActivity.class);
        else{
            //如果用户登录了
            if(((BaseApplication)getApp()).isLogin() == null){
                startActExit(MainActivity.class);
            }else{
                startActExit(LoginActivity.class);
            }
        }

        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
}
